<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>2_受控组件.html</title>
    <style>
        .input_label {
            margin-left: 10px;
        }
    </style>
</head>
<body>
<!--准备好一个容器-->
<div id="test">

</div>
</body>

<!--核心库引入顺序，必须先是核心库然后在引入dom库-->
<script src="../js/react.development.js" crossorigin></script>
<!--用于支持react操作dom-->
<script src="../js/react-dom.development.js"></script>
<!--引入babel，用于将jsx转为js-->
<script src="../js/babel.min.js"></script>

<script type="text/babel">
    class Login extends React.Component {
        //初始化状态
        state = {
            username: '',
            password: ''
        }

        handSubmit = (event) => {
            event.preventDefault()//阻止表单提交
            let {username,password} = this.state;
            alert(`用户：${username},密码：${password}`)
        }

        //保存用户名到状态中
        saveUsername = (event) => {
            console.log('@', event.target.value)
            this.setState({username: event.target.value})
        }
        //保存密码到状态中
        savePassword = (event) => {
            console.log('@', event.target.value)
            this.setState({password: event.target.value})
        }

        render() {
            return (
                <form onSubmit={this.handSubmit}>
                    <label>
                        用户名：<input type="text" onChange={this.saveUsername} name="username"/><br/>
                    </label>
                    <label>
                        密码：<input type="password" onChange={this.savePassword} name="password"/><br/>
                    </label>
                    <button>登陆</button>
                </form>
            )
        }
    }

    //2·渲染组件
    ReactDOM.render(<Login/>, document.getElementById('test'))
</script>
</html>